package com.ruoyi.classai.api;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.classai.domain.EwcChapter;
import com.ruoyi.classai.domain.EwcClass;
import com.ruoyi.classai.domain.EwcCourse;
import com.ruoyi.classai.domain.EwcDepartment;
import com.ruoyi.classai.domain.dto.*;
import com.ruoyi.classai.domain.vo.EwcChapterVo;
import com.ruoyi.classai.domain.vo.EwcCourseVo;
import com.ruoyi.classai.service.*;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * 系统管理接口
 * @author lcj
 * @date 2025年02月27日 15:14
 */
@Slf4j
@Validated
@RestController
@Tag(name = "系统管理接口", description = "班级管理、学生管理、课程管理模块接口...")
@RequestMapping("/api/system")
public class SystemManageApi extends BaseController {

    @Autowired
    private IEwcClassService classService;

    @Autowired
    private IEwcDepartmentService departmentService;

    @Autowired
    private IEwcCourseService courseService;

    @Autowired
    private IEwcStudentService studentService;

    @Autowired
    private IEwcChapterService chapterService;

    @Autowired
    private IEwcClassCourseService classCourseService;


    /**
     * 查询学校院系列表
     * @return 院系列表
     */
    @Operation(summary = "查询学校院系列表")
    @GetMapping("/getSchoolDepartmentList")
    public AjaxResult getSchoolDepartmentList(){
        List<EwcDepartment> departments=departmentService.list(Wrappers.<EwcDepartment>lambdaQuery().eq(EwcDepartment::getSchoolId, ThirdSessionHolder.getThirdSession().getSchoolId())
                .eq(EwcDepartment::getStatus, Constants.COMMON_SUCCESS));
        return AjaxResult.success(departments);
    }

    /**
     * 添加班级信息
     * @param ewcClassDto 班级信息
     * @return AjaxResult
     */
    @Operation(summary = "添加班级信息",tags = "新开班级")
    @PostMapping("/addClass")
    public AjaxResult addClass(@Valid @RequestBody EwcClassDto ewcClassDto){
        EwcClass ewcClass=new EwcClass();
        BeanUtils.copyProperties(ewcClassDto,ewcClass);
        classService.addClass(ewcClass);
        return AjaxResult.success("success");
    }

    /**
     * 修改班级信息
     * @param ewcClassDto 班级信息
     * @return AjaxResult
     */
    @Operation(summary = "修改班级信息",tags = "班级管理")
    @PostMapping("/updateClass")
    public AjaxResult updateClass(@Valid @RequestBody EwcClassDto ewcClassDto){
        EwcClass ewcClass=new EwcClass();
        BeanUtils.copyProperties(ewcClassDto,ewcClass);
        classService.updateById(ewcClass);
        return AjaxResult.success("success");
    }

    /**
     * 删除班级信息
     * @param id 班级id
     * @return AjaxResult
     */
    @Operation(summary = "删除班级信息",tags = "班级管理")
    @GetMapping("/delteClassCourse")
    public AjaxResult delteClassCourse(@NotBlank(message = "班级id不能为空") String id){
        classService.deleteClassCourse(id);
        return AjaxResult.success("success");
    }

    /**
     * 查询班级详情
     * @param classId 班级id
     * @return AjaxResult
     */
    @Operation(summary = "查询班级详情",tags = "班级管理")
    @GetMapping("/getClassById")
    public AjaxResult getClassById(@NotBlank(message = "参数不能为空") String classId){

        return  AjaxResult.success("查询成功",classService.getClassById(classId));
    }

    /**
     * 查询班级列表
     * @param className 课程名称
     * @param classType 班级类型
     * @return AjaxResult
     */
    @Operation(summary = "查询班级列表",tags = "班级管理")
    @GetMapping("/getClassList")
    public AjaxResult getClassList(String className,Integer classType){
        return  AjaxResult.success("查询成功",classService.getClassList(className,classType));
    }

    /**
     * 查询学生列表
     * @param id 班级id
     * @return AjaxResult
     */
    @Operation(summary = "查询学生列表",tags = "班级管理")
    @GetMapping("/getClassStudentList")
    public TableDataInfo getClassStudentList(@NotBlank(message = "班级id不能为空") String id){
        startPage();
        List<EwcStudentDto> list =studentService.getList(null,id);
        return getDataTable(list);
    }

    /**
     * 查询课程列表
     * @param courseName 课程名称
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param classId 班级id
     * @return AjaxResult
     */
    @Operation(summary = "查询课程列表",tags = "课程管理模块")
    @GetMapping("/getCourseList")
    public TableDataInfo getCourseList(String courseName,String classId,String startTime,String endTime){
        startPage();
        List<EwcCourseVo> list = courseService.getList(ThirdSessionHolder.getThirdSession().getId(),courseName,startTime,endTime,classId);
        return getDataTable(list);
    }

    /**
     * 查询课程详情
     * @param courseId 课程ID
     * @return AjaxResult
     */
    @Operation(summary = "查询课程详情",tags = "课程管理模块")
    @GetMapping("/getCourseById")
    public AjaxResult getCourseById(@NotBlank(message = "参数不能为空") String courseId){

        EwcCourse course = courseService.getById(courseId);
        return AjaxResult.success("success",course);
    }


    /**
     * 查询关联班级课程
     * @param classId 班级ID
     * @return AjaxResult
     */
    @Operation(summary = "查询关联班级课程",tags = "班级管理模块")
    @GetMapping("/getCourseAssociation")
    public AjaxResult getCourseAssociation(@NotBlank(message = "课程ID不能为空") String classId){

        return AjaxResult.success("success",courseService.getCourseAssociation(classId));
    }

    /**
     * 添加关联班级课程
     * @param classCourseDto 班级课程关联
     * @return AjaxResult
     */
    @Operation(summary = "添加关联班级课程",tags = "班级管理模块")
    @PostMapping("/addRelatedCourses")
    public AjaxResult addRelatedCourses(@Valid @RequestBody ClassCourseDto classCourseDto){
        courseService.addRelatedCourses(classCourseDto);
        return AjaxResult.success("success");
    }

    /**
     * 删除关联班级课程
     * @param courseId 课程ID
     * @param classId 班级ID
     * @return AjaxResult
     */
    @Operation(summary = "删除关联班级课程",tags = "班级管理模块")
    @GetMapping("/deleteRelatedCourses")
    public AjaxResult addRelatedCourses(@NotBlank(message = "课程ID不能为空")String courseId,
                                        @NotBlank(message = "班级ID不能为空")String classId){

        courseService.deleteRelatedCourses(courseId,classId);
        return AjaxResult.success("success");
    }

    /**
     * 新开课程
     * @param courseDto 课程信息
     * @return AjaxResult
     */
    @Operation(summary = "新开课程")
    @PostMapping("/addCourse")
    public AjaxResult addCourse(@Valid @RequestBody EwcCourseDto courseDto){
        courseService.addCourse(courseDto);
        return AjaxResult.success("success");
    }

    /**
     * 老师班级列表
     * @return AjaxResult
     */
    @Operation(summary = "老师班级列表")
    @GetMapping("/getTeacherClassList")
    public AjaxResult getTeacherClassList(){
        List<EwcClass> classList=classService.list(Wrappers.<EwcClass>lambdaQuery().eq(EwcClass::getTeacherId,ThirdSessionHolder.getThirdSession().getId())
                .ne(EwcClass::getStatus,Constants.COMMON_DELETE));
        return AjaxResult.success("success",classList);
    }

    /**
     * 修改发布课程信息
     * @param courseDto 课程信息
     * @return AjaxResult
     */
    @Operation(summary = "修改发布课程信息",tags = "课程管理模块")
    @PostMapping("/updateCourse")
    public AjaxResult updateCourse(@Valid @RequestBody EwcCourseDto courseDto){

        courseService.updateCourse(courseDto);
        return AjaxResult.success("success");
    }

    /**
     * 删除发布课程信息
     * @param id 课程id
     * @return AjaxResult
     */
    @Operation(summary = "删除发布课程信息",tags = "课程管理模块")
    @GetMapping("/deleteCourse")
    public AjaxResult deleteCourse(@NotBlank(message = "参数不能为空") String id){
        courseService.deleteCourse(id);
        return AjaxResult.success("success");
    }

    /**
     * 查询章节列表
     * @param courseId 课程ID
     * @param chapterName 章节名称
     * @param classId 班级ID
     * @return AjaxResult
     */
    @Operation(summary = "查询章节列表",tags = "课程管理模块")
    @GetMapping("/getChapterList")
    public TableDataInfo getChapterList(@NotBlank(message = "课程ID不能为空") String courseId,String classId,
                                        String chapterName){
        startPage();
        List<EwcChapterVo> list = chapterService.getChapterList(courseId,classId,chapterName);
        return getDataTable(list);
    }

    /**
     * 统计章节状态数量
     * @param courseId 课程ID
     * @param classId 班级ID
     * @return AjaxResult
     */
    @Operation(summary = "统计章节状态数量",tags = "课程管理模块")
    @GetMapping("/countChapterNum")
    public AjaxResult countChapterNum(@NotBlank(message = "课程ID不能为空")String courseId, String classId){
        //章节数量
        long total=chapterService.count(Wrappers.<EwcChapter>lambdaQuery().eq(EwcChapter::getCourseId,courseId).eq(EwcChapter::getClassId,classId).ne(EwcChapter::getStatus,0));
        //已授课
        long skNum=chapterService.count(Wrappers.<EwcChapter>lambdaQuery().eq(EwcChapter::getCourseId,courseId).eq(EwcChapter::getClassId,classId).eq(EwcChapter::getStatus,3));
        //已备课
        long bkNum=chapterService.count(Wrappers.<EwcChapter>lambdaQuery().eq(EwcChapter::getCourseId,courseId).eq(EwcChapter::getClassId,classId).eq(EwcChapter::getStatus,2));
        //未备课
        long wbkNum=chapterService.count(Wrappers.<EwcChapter>lambdaQuery().eq(EwcChapter::getCourseId,courseId).eq(EwcChapter::getClassId,classId).eq(EwcChapter::getStatus,1));
        //查询课程
        EwcCourse course=courseService.getById(courseId);
        JSONObject resJson=new JSONObject().set("total",total).set("skNum",skNum).set("bkNum",bkNum)
                .set("wbkNum",wbkNum).set("course",course).set("classId",classId);
        return AjaxResult.success("success",resJson);
    }

    /**
     * 添加章节
     * @param chapterDto 章节信息
     * @return AjaxResult
     */
    @Operation(summary = "添加章节",tags = "课程管理模块")
    @PostMapping("/addChapter")
    public AjaxResult addChapter(@Valid @RequestBody EwcChapterDto chapterDto){
        EwcChapter chapter=new EwcChapter();
        BeanUtils.copyProperties(chapterDto,chapter);
        //添加章节
        chapter.setStatus(1);
        chapterService.save(chapter);
        return AjaxResult.success("success");
    }

    /**
     * 修改章节
     * @param chapterDto 章节信息
     * @return AjaxResult
     */
    @Operation(summary = "修改章节",tags = "课程管理模块")
    @PostMapping("/updateChapter")
    public AjaxResult updateChapter(@Valid @RequestBody EwcChapterDto chapterDto){
        EwcChapter chapter=new EwcChapter();
        BeanUtils.copyProperties(chapterDto,chapter);
        chapterService.updateById(chapter);
        return AjaxResult.success("success");
    }


    /**
     * 根据章节ID查询章节详情
     * @param chapterId 章节ID
     * @return AjaxResult
     */
    @Operation(summary = "根据章节ID查询章节详情",tags = "课程管理模块")
    @PostMapping("/getChapterById")
    public AjaxResult getChapterById(@NotBlank(message = "章节ID不能为空") String chapterId){
        return AjaxResult.success("success",  chapterService.getById(chapterId));
    }

    /**
     * 删除章节
     * @param id 章节ID
     * @return AjaxResult
     */
    @Operation(summary = "删除章节",tags = "课程管理模块")
    @GetMapping("/deleteChapter")
    public AjaxResult deleteChapter(@NotBlank(message = "参数不能为空") String id){
        EwcChapter chapter=new EwcChapter();
        chapter.setId(id);
        chapter.setStatus(0);
        chapterService.updateById(chapter);
        return AjaxResult.success("success");
    }


    /**
     * 学生信息添加
     * @param ewcStudentDto 学生信息
     * @return AjaxResult
     */
    @Operation(summary = "学生信息添加",tags = "课程管理模块")
    @PostMapping("/addStudent")
    public AjaxResult addStudent(@Valid @RequestBody EwcStudentDto ewcStudentDto){
        studentService.add(ewcStudentDto);
        return AjaxResult.success("success");
    }

    /**
     * 学生信息更新
     * @param ewcStudentDto 学生信息
     * @return AjaxResult
     */
    @Operation(summary = "学生信息更新",tags = "课程管理模块")
    @PostMapping("/updateStudent")
    public AjaxResult updateStudent(@Valid @RequestBody EwcStudentDto ewcStudentDto){
        studentService.edit(ewcStudentDto);
        return AjaxResult.success("success");
    }

    /**
     * 删除学生
     * @param studentId 学生id
     * @param classId 班级id
     * @return AjaxResult
     */
    @Operation(summary = "删除学生",tags = "课程管理模块")
    @GetMapping("/deleteStudent")
    public AjaxResult deleteStudent(@NotBlank(message = "学生ID不能为空") String studentId,@NotBlank(message = "班级ID不能为空") String classId){
        studentService.deleteStudent(studentId,classId);
        return AjaxResult.success("success");
    }

    /**
     * 导入学生信息
     * @param file 学生excel文件
     * @param classId 班级id
     * @return AjaxResult
     */
    @Operation(summary = "学生信息导入",tags = "课程管理模块")
    @PostMapping("/importStudent")
    public AjaxResult importStudent(@NotNull(message = "文件不能为空") MultipartFile file,@NotBlank(message = "班级ID不能为空")String classId) throws IOException {
        return AjaxResult.success("success",studentService.importStudent(file,classId));
    }


}
